package com.nuotu.datawall.clouddata.layoutData;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.nuotu.datawall.clouddata.R;
import com.nuotu.datawall.clouddata.been.RtInfo;
import com.nuotu.datawall.clouddata.control.Token;
import com.nuotu.datawall.clouddata.util.JsonUtil;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.Tool;
import com.nuotu.datawall.clouddata.view.EchartView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class NewtLayout {
    private static volatile NewtLayout instance;
    private static Context mContext;
    private static ViewFlipper viewFlipper;
    private int tempSize=0;
    public static NewtLayout getInstance(Context context) {
        if (instance == null) {
            synchronized (NewtLayout.class) {
                if (instance == null) {
                    mContext=context;
                    instance = new NewtLayout();
                }
            }
        }
        return  instance;
    }
    public NewtLayout init(ViewFlipper viewFlipper){
        this.viewFlipper=viewFlipper;
        viewFlipper.setFlipInterval(1000*10);
        viewFlipper.startFlipping();
        return instance;
    }
    private void setView(List list){
        viewFlipper.removeAllViews();
        for (Object obj:list){
            View view = ((Activity)mContext).getLayoutInflater().inflate(R.layout.news_item,null);
            ((TextView)view.findViewById(R.id.text_news)).setText(obj+"");
            viewFlipper.addView(view);
        }
    }

    public void refreshData(){
        RequestParams params = Tool.getRequestParams(mContext, ServerConfig.POST_GETNEW_URL_KEY);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                RtInfo rtInfo= JsonUtil.fromJson(result, RtInfo.class);
                setView((List) rtInfo.getData());
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Token.getInstance(mContext).refreshToken(ex.toString());
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });
    }

}
